<template>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="一级分类名称">
        <el-input v-model="form.name" />
        <el-button type="primary" size="default" @click="subIncre">
            + 字标题
        </el-button>
        
      </el-form-item>
      <el-form-item v-for="(item,index) in form.sub_title" :label="`子标题${index+1}`">
        <el-input v-model="form.sub_title[index]" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="onSubmit">新增</el-button>
        <el-button @click="categorySave">转存数据</el-button>
      </el-form-item>
    </el-form>
  </template>
  
  <script setup>
  import { reactive,ref } from 'vue'
  import request from '../../utils/request.js'
import { categoryPost } from '../../api/category.js';
  
  // do not use same name with ref
  const form = reactive({
    name: '',
    sub_title:[]  //控制字标题数量
  })
  
  const onSubmit = () => {
    console.log(form)
    // 录入固定数据
    // request.post('/classes/Category',{
    //     name:'家具分类'
    // })

    //录入表单数据
    request.post('/classes/Category',form)
  }

//   新增字标题输入框数量
  const subIncre = ()=>{
    form.sub_title.push('')
  }

  // 数据转存
  const categorySave = ()=>{
    categoryPost()
  }
  </script>
  